<script setup lang="ts">
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";

const ruleFormRef = ref();
//自定义规则
const validatePass = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请输入密码"));
  } else {
    if (ruleForm.checkPass !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass");
    }
    callback();
  }
};
const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请再次输入密码"));
  } else if (value !== ruleForm.pass) {
    callback(new Error("两次输入的密码不同"));
  } else {
    callback();
  }
};

const ruleForm = reactive({
  pass: "",
  checkPass: ""
});

const rules = ref({
  pass: [{ validator: validatePass, trigger: "blur" }],
  checkPass: [{ validator: validatePass2, trigger: "blur" }]
});

//提交
const submitForm = formEl => {
  if (!formEl) return;
  formEl.validate(valid => {
    if (valid) {
      //验证成功后
    }
  });
};
// 取消
const resetForm = () => {};

//验证码
const input = ref("");
</script>

<template>
  <div>
    <el-text size="large">修改密码</el-text>

    <div style="margin: 100px 0 0 300px">
      <el-card style="max-width: 480px">
        <el-text size="large">手机号：{{ 123456789987 }}</el-text>
        <br />
        <span>手机验证码：</span>
        <el-input
          v-model="input"
          style="width: 240px"
          placeholder="请输入验证码"
          clearable
        />
        <el-button type="primary" plain>发送验证码</el-button>
        <br />

        <el-form
          ref="ruleFormRef"
          style="max-width: 600px"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="auto"
        >
          <br />
          <br />
          <el-form-item label="新密码:" prop="pass">
            <el-input
              v-model="ruleForm.pass"
              type="password"
              autocomplete="off"
            />
          </el-form-item>

          <el-form-item label="确认密码:" prop="checkPass">
            <el-input
              v-model="ruleForm.checkPass"
              type="password"
              autocomplete="off"
            />
          </el-form-item>

          <el-form-item style="float: right">
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              保存
            </el-button>
            <el-button @click="resetForm()">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<style lang="scss" scoped></style>
